package com.tange.ai.core.demo.player;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tange.ai.core.demo.Utils;
import com.tange.ai.core.demo.databinding.ActivityPlayerBinding;
import com.tange.ai.core.demo.setting.SettingActivity;
import com.tange.core.data.structure.Device;

import java.util.ArrayList;
import java.util.List;

public class PlayerActivity extends AppCompatActivity {

    private static final String PARAM_DEVICE_ITEM = "PARAM_DEVICE_ITEM";

    private ActivityPlayerBinding binding;

    private Device device;

    private final List<BaseViewDelegate> viewDelegateList = new ArrayList<>();

    public static void launch(Context context, Device device) {
        Intent intent = new Intent(context, PlayerActivity.class);
        intent.putExtra(PARAM_DEVICE_ITEM, device);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityPlayerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportActionBar().hide();
        Utils.setDarkStatusBar(this, Color.TRANSPARENT);

        device = (Device) getIntent().getSerializableExtra(PARAM_DEVICE_ITEM);

        if (device == null) {
            Toast.makeText(this, "设备信息为空", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        binding.name.setText(device.getName());
        binding.setting.setOnClickListener(v -> SettingActivity.launch(v.getContext(), device));


        PlayerViewDelegate playerViewDelegate = new PlayerViewDelegate(this, binding, device);

        viewDelegateList.add(playerViewDelegate);

        for (BaseViewDelegate viewDelegate : viewDelegateList) {
            viewDelegate.onCreate();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        for (BaseViewDelegate viewDelegate : viewDelegateList) {
            viewDelegate.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        for (BaseViewDelegate viewDelegate : viewDelegateList) {
            viewDelegate.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        for (BaseViewDelegate viewDelegate : viewDelegateList) {
            viewDelegate.onDestroy();
        }
    }
}